package com.june.junemplayer.mapper;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import com.june.junemplayer.pojo.Song;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class SongListContentMapper {

    public void insert(Context context, String listTitle, String songPath, int number) {
        SongListContentSQLiteOpenHelper songListContentSQLiteOpenHelper = new SongListContentSQLiteOpenHelper(context, "MusicPlayerDatabase.db", null, SongListContentSQLiteOpenHelper.getVersion());
        SQLiteDatabase database = songListContentSQLiteOpenHelper.getWritableDatabase();
        ContentValues value = new ContentValues();
        value.put("title", listTitle);
        value.put("path", songPath);
        value.put("number", number);
        Log.d("JuneSQLite", "--SongListContent--插入--" + value);
        database.insert("SongListContent", null, value);
    }

    public int delete(Context context, String listTitle, String songPath) {
        SongListContentSQLiteOpenHelper songListContentSQLiteOpenHelper = new SongListContentSQLiteOpenHelper(context, "MusicPlayerDatabase.db", null, SongListContentSQLiteOpenHelper.getVersion());
        SQLiteDatabase database = songListContentSQLiteOpenHelper.getWritableDatabase();
        Log.d("JuneSQLite", "----SongListContent----删除----title = " + listTitle + " , path = " + songPath);
        String[] strings = new String[]{listTitle, songPath};
        return database.delete("SongListContent", "title = ? and path = ? ", strings);
    }

    public void update(Context context, String listTitle, String songPath, int number) {
        Log.d("JuneSQLite", "----SongListContent----update() start:songPath = " + listTitle + ",number = " + songPath+"----");
        if (delete(context, listTitle, songPath) == -1) {
            Log.d("JuneSQLite", "----SongListContent----删除失败----");
            Log.d("JuneSQLite", "----SongListContent----更新失败----");
            return;
        }
        insert(context, listTitle, songPath, number);
        Log.d("JuneSQLite", "----SongListContent----update() end----");
    }

    @SuppressLint("Range")
    public ArrayList<String> retrieveSongsBySongListTitle(Context context, String listTitle) {
        Log.d("JuneSQLite", "----SongListContent----retrieveSongsBySongListTitle() start:listTitle = " + listTitle+"----");
        SongListContentSQLiteOpenHelper songListContentSQLiteOpenHelper = new SongListContentSQLiteOpenHelper(context,
                "MusicPlayerDatabase.db", null, SongListContentSQLiteOpenHelper.getVersion());
        SQLiteDatabase database = songListContentSQLiteOpenHelper.getWritableDatabase();
        String[] titles = new String[]{listTitle};
        Cursor cursor = database.query("SongListContent", null, "title = ? ",
                titles, null, null, null);
        ArrayList<String> songPaths = new ArrayList<>();
        int count=0;
        if (cursor.moveToFirst()) {
            do {
                String songPath = cursor.getString(cursor.getColumnIndex("path"));
                Log.d("JuneSQLiteInfo", count + "----" + songPath);
                songPaths.add(songPath);
            } while (cursor.moveToNext());
        }
        cursor.close();
        Log.d("JuneSQLite", "----SongListContent----retrieveSongsBySongListTitle() end:count = "+count+"----");
        return songPaths;
    }

    @SuppressLint("Range")
    public ArrayList<String> retrieveSongListsBySongPath(Context context, String songPath) {
        Log.d("JuneSQLite", "----SongListContent----retrieveSongListsBySongPath() start:songPath = " + songPath+"----");
        SongListContentSQLiteOpenHelper songListContentSQLiteOpenHelper = new SongListContentSQLiteOpenHelper(context,
                "MusicPlayerDatabase.db", null, SongListContentSQLiteOpenHelper.getVersion());
        SQLiteDatabase database = songListContentSQLiteOpenHelper.getWritableDatabase();
        String[] paths = new String[]{songPath};
        Cursor cursor = database.query("SongListContent", null, "path = ? ",
                paths, null, null, null);
        ArrayList<String> listTitles = new ArrayList<>();
        int count=0;
        if (cursor.moveToFirst()) {
            do {
                String listTitle = cursor.getString(cursor.getColumnIndex("title"));
                Log.d("JuneSQLiteInfo", count + "----" + songPath);
                listTitles.add(listTitle);
            } while (cursor.moveToNext());
        }
        cursor.close();
        Log.d("JuneSQLite", "----SongListContent----retrieveSongListsBySongPath() end:count = "+count+"----");
        return listTitles;
    }

    public static class SongListContentSQLiteOpenHelper extends SQLiteOpenHelper {

        private static int mVersion = 1;
        String create = "create table SongListContent(title integer,path integer,number integer,primary key(title,path))";

        public SongListContentSQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
            mVersion = version == 0 ? 1 : version;
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.execSQL(create);
            Log.d("JuneSQLite", "创建成功----SongListContent----版本" + sqLiteDatabase.getVersion());
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
//            sqLiteDatabase.execSQL(create);
            Log.d("JuneSQLite", "更新成功----SongListContent----版本" + oldVersion + "  ->  " + newVersion);
            mVersion = newVersion;
        }

        public static int getVersion() {
            return mVersion;
        }
    }
}